#include "adminlogin.h"
#include "ui_adminlogin.h"
#include <QMessageBox>

AdminLogin::AdminLogin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AdminLogin)
{
    ui->setupUi(this);
    this->myDatabase.getConnection();
}

AdminLogin::~AdminLogin()
{
    delete ui;
}

void AdminLogin::on_Register_clicked()
{
    QString adminInputAccount = ui->Edit_account->text();
    QString adminInputPassword = ui->Edit_password->text();
    //判断用户名和密码是否为空
    if(adminInputAccount==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Account can't be NULL!");
    }
    else if (adminInputPassword==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Password can't be NULL!");
    }
    else
    {
        QSqlQuery sql;

        sql.prepare("SELECT * FROM tb_admin WHERE account = :adminInputAccount");
        sql.bindValue(":adminInputAccount", adminInputAccount);
        sql.exec();

        if(sql.first()){
        //如果用户名重复，则提示用户名重复
            QMessageBox::warning(this,"ERROR","用户名重复");//如果用户名重复，则提示用户名重复
        }
        else{
        //如果用户名不重复，添加数据进入数据库
            sql.prepare("INSERT INTO tb_admin(account,password) values(:adminInputAccount,:adminInputPassword)");
            sql.bindValue(":adminInputAccount", adminInputAccount);
            sql.bindValue(":adminInputPassword", adminInputPassword);
            sql.exec();
            QMessageBox::information(this,"提示","注册成功！！",QMessageBox::Yes);
        }
    }
}


void AdminLogin::on_Quit_clicked()
{
    this->~AdminLogin();
}


void AdminLogin::on_Login_clicked()
{
    QString adminInputAccount = ui->Edit_account->text();
    QString adminInputPassword = ui->Edit_password->text();
    //判断用户名和密码是否为空
    if(adminInputAccount==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Account can't be NULL!");
    }
    else if (adminInputPassword==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Password can't be NULL!");
    }
    else
    {
        QSqlQuery sql;

        sql.prepare("SELECT * FROM tb_admin WHERE account = :adminInputAccount");
        sql.bindValue(":adminInputAccount", adminInputAccount);
        sql.exec();

        bool nFlag = 0;
        QString adminAccount;
        QString adminPassword;
        while(sql.next()){
            adminAccount = sql.value("account").toString();
            adminPassword = sql.value("password").toString();
            qDebug()<<adminAccount;
            qDebug()<<adminPassword;
            if(adminInputAccount == adminAccount && adminInputPassword == adminPassword){
                int aid = sql.value("id").toInt();
                AdminView* pAdminView = new AdminView(this,aid);
                nFlag = 1;
                this->close();
                this->pMainWindow->close();
                pAdminView->show();
            }
        }
        if(!nFlag)
            QMessageBox::information(this,"提示","登陆失败，请重试！",QMessageBox::Yes);
    }
}

